package com.kl.modular.system.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.kl.common.util.BeanMapperUtil;
import com.kl.common.util.StringUtil;
import com.kl.modular.base.service.impl.BaseServiceImpl;
import com.kl.modular.system.dao.mapper.SystemDicMapper;
import com.kl.modular.system.dao.model.SystemDic;
import com.kl.modular.system.dao.model.SystemDicType;
import com.kl.modular.system.service.api.ISystemDicService;
import com.kl.modular.system.service.api.ISystemDicTypeService;
import com.kl.modular.system.service.bo.SystemDicBo;
import com.kl.modular.system.service.vo.SystemDicSortNoComparator;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * 数据字典serviceImpl业务实现
 * @author xr_zjb
 * @date 2017-08-07 15:58
 * @(C) xinrui Copyright 
 */
@Service
public class SystemDicServiceImpl extends BaseServiceImpl<SystemDicMapper, SystemDic> implements ISystemDicService {

    @Autowired
    private ISystemDicTypeService systemDicTypeService;

    /**
     * 模糊查询数据字典(字典类型，名称模糊，值)
     * @param page 分页数据
     * @param systemDicBo 数据对象BO类
     * @return
     */
    public Page<SystemDic> selectPageBySystemDicBo(Page<SystemDic> page, SystemDicBo systemDicBo) throws Exception {
        if(page == null)page = new Page<SystemDic>();
        if(systemDicBo == null)return page;
        Wrapper<SystemDic> wrapper = new EntityWrapper<SystemDic>();
        if (systemDicBo.getDicTypeId() != null) {
            wrapper.eq("dic_type_id", systemDicBo.getDicTypeId());
        }
        if (!StringUtil.StringIsNullOrEmpty(systemDicBo.getDicValue())) {
            wrapper.eq("`dic_value`", systemDicBo.getDicValue());
        }
        if (!StringUtil.StringIsNullOrEmpty(systemDicBo.getName())) {
            wrapper.like("name", systemDicBo.getName());
        }
        wrapper.eq("enabled", false);
        page = selectPage(page, wrapper);
        setPageDicType(page);
        return page;
    }

    /**
     * 分页查询 (包含字典类型数据)
     * @param dicBo
     * @return
     */
    public Page<SystemDic> selectPageByBo(Page<SystemDic> dicPage, SystemDicBo dicBo) throws Exception {
        Page<SystemDic> dicPageData = super.selectPageByBo(dicPage, dicBo);
        setPageDicType(dicPageData);
        return dicPageData;
    }

    /**
     * 设置Page对象中的DicType对象
     * @param dicPageData 要设置的Page对象
     * @throws Exception
     */
    public void setPageDicType(Page<SystemDic> dicPageData) throws Exception {
        if (dicPageData != null && dicPageData.getRecords() != null) {
            List<Long> dicTypeIdList = BeanMapperUtil.getFieldList(dicPageData.getRecords(), "dicTypeId", Long.class);
            if (dicTypeIdList.size() > 0) {
                List<SystemDicType> systemDicTypes = systemDicTypeService.selectBatchIds(dicTypeIdList);
                Map<Long, SystemDicType> objectSystemDicTypeMap = BeanMapperUtil.list2Map(systemDicTypes, "id", Long.class);
                for (int i = 0; i < dicPageData.getRecords().size(); i++) {
                    dicPageData.getRecords().get(i).setDicType(objectSystemDicTypeMap.get(dicPageData.getRecords().get(i).getDicTypeId()));
                }
            }
        }
    }

    /**
     * 字典列表查询
     * @param dicType 字典类别
     * @return
     */
    public List<SystemDic> selectListByDicType(String dicType) {
        if(StringUtils.isEmpty(dicType)){
            return null;
        }
        SystemDicType systemDicType = new SystemDicType();
        systemDicType.setTypeCode(dicType);
        systemDicType = systemDicTypeService.selectOneByEntity(systemDicType);
        if(systemDicType == null){
            return null;
        }
        SystemDic dic = new SystemDic();
        dic.setDicTypeId(systemDicType.getId());
        List<SystemDic> dicList = selectListByEntity(dic);
        SystemDicSortNoComparator comparator = new SystemDicSortNoComparator();
        Collections.sort(dicList, comparator);
        return dicList;
    }

    /**
     * 字典map查询
     * @param dicType
     * @return
     */
    public Map<String, SystemDic> selectMapByDicType(String dicType) {
        List<SystemDic> systemDicList = selectListByDicType(dicType);
        Map<String ,SystemDic> systemDicMap = null;
        try {
            systemDicMap = BeanMapperUtil.list2Map(systemDicList, "dicValue", String.class);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return systemDicMap;
    }

    /**
     * 查找数据字典数据
     * @param dicTypeCode
     * @param dicValue
     * @return
     */
    public SystemDic findSystemDic(String dicTypeCode, String dicValue) {
        SystemDicType dicType = new SystemDicType();
        dicType.setTypeCode(dicTypeCode);
        dicType = systemDicTypeService.selectOneByEntity(dicType);
        if(dicType == null){
            return null;
        }
        SystemDic systemDic = new SystemDic();
        systemDic.setDicTypeId(dicType.getId());
        systemDic.setDicValue(dicValue);
        systemDic = selectOneByEntity(systemDic);
        return systemDic;
    }
}
